package leetcode;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int val){
        this.val = val;
    }

    @Override
    public String toString() {
        // 按层遍历
        if(this == null) return "";
        List<List<Integer>> hasDealList = new ArrayList<>();
        List<TreeNode> toDealList = new ArrayList<>();
        toDealList.add(this);
        while(toDealList != null && !toDealList.isEmpty()){
            List<TreeNode> tempList = new ArrayList<>();
            List<Integer> vals = new ArrayList<>();
            for(int i = 0; i < toDealList.size(); i++){
                TreeNode node = toDealList.get(i);
                vals.add(node.val);
                if(node.left != null){tempList.add(node.left);}
                if(node.right != null){tempList.add(node.right);}
            }
            hasDealList.add(new ArrayList(vals));
            toDealList = new ArrayList(tempList);
        }
        return hasDealList.toString();
    }
}
